// components/checkbox/checkbox.js
import formatDate from '../../utils/format_date';

Component({

    /**
     * 组件的属性列表
     */
    properties: {
        activeColor: {
            type: String,
            value: '#ec7044',
        },
        disableText: {
            type: String,
            value: '已订',
        },
        list: {
            type: Array,
            value: []
        },
        prevent: {
            type: Boolean,
            value: false
        },
    },

    /**
     * 组件的初始数据
     */
    data: {

    },

    lifetimes: {
        created: function () {
            this.timer = [];
        },
        attached: function () {},
        ready: function () {},
        detached: function () {},
    },

    /**
     * 组件的方法列表
     */
    methods: {
        boxClick(e) {
            let index = e.currentTarget.dataset.index;
            console.log(this.data.list[index])
            if (!this.data.list[index].selected) {
                let now = new Date();
                let endTime = this.data.list[index].endTime;
                // if (!endTime) {
                //     let time = this.data.list[index].time.split('-')[1];
                //     endTime = formatDate.formatTime(now, 'yyyy-MM-dd') + ' ' + time + ':00';
                // }
                console.log(endTime)
                if (now.getTime() > new Date(endTime).getTime()) {
                    wx.showToast({
                        icon: 'none',
                        title: '预订时间已过'
                    })
                    return;
                }
            }
            this.setData({
                [`list[${index}].selected`]: !this.data.list[index].selected
            })
            let obj = {
                lastId: this.data.list[index].id,
                lastIndex: index,
                lastSelected: this.data.list[index].selected
            };
            if (this.data.list[index].selected) {
                this.timer.push(this.data.list[index].id);
            } else {
                let id = this.data.list[index].id;
                let idx = this.timer.indexOf(id);
                if (idx != -1) {
                    this.timer.splice(idx, 1);
                }
            }
            obj.value = this.timer;
            this.triggerEvent('change', obj);
        }
    }
})